package com.example.hello;

import com.example.hello.POJO.BadgePOJO;
import com.example.hello.views.badges.Badges;
import com.vaadin.event.LayoutEvents;
import com.vaadin.server.Page;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class Badge extends VerticalLayout implements Comparable {

    private Label label;
    private String name;
    private String color;

    public Badge(int content, String color) {
        this(content + "", "", color);
    }

    public Badge(String content, String color) {
        this(content, "", color);
    }

    public Badge(String content, String name, String color) {
        setName(name);
        setColor(color);

        setWidth("30px");
        setHeight("30px");

        label = new Label(content);
        label.setSizeFull();
        label.setStyleName("badge-text");
        label.setWidth("30px");
        label.setHeight("15px");

        addComponent(label);
        setComponentAlignment(label, Alignment.MIDDLE_CENTER);

        Page.Styles styles = Page.getCurrent().getStyles();
        styles.add(".helloVG .badge-text {" +
                "color: white;" +
                "font-size: 12pt;" +
                "text-align: center;" +
                "line-height: 15px !important; }");

        addLayoutClickListener(new LayoutEvents.LayoutClickListener() {
            @Override
            public void layoutClick(LayoutEvents.LayoutClickEvent event) {
                getUI().getNavigator().navigateTo(Badges.NAME + "/" + getDecodedName(false));
            }
        });
    }

    public String getContent() {
        return label.getValue();
    }

    public void setContent(String content) {
        label.setValue(content);
    }

    public void setContent(int content) {
        label.setValue(content + "");
    }

    public String getName() {
        return getDecodedName(true);
    }

    public String getDecodedName(boolean decode) {
        if (decode) {
            try {
                return URLDecoder.decode(name, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return name;
            }
        } else {
            return name;
        }
    }

    public void setName(String name) {
        this.name = name;
        setDescription(getName());
    }

    public String getColor() {
        return color;
    }

    public int[] getHexColor() {
        int[] rgb = new int[3];
        rgb[0] = Integer.parseInt(color.substring(0, 2), 16);
        rgb[1] = Integer.parseInt(color.substring(2, 4), 16);
        rgb[2] = Integer.parseInt(color.substring(4, 6), 16);
        return rgb;
    }

    public void setColor(String color) {
        this.color = color;

        setStyleName("badge-background-" + color);

        Page.Styles styles = Page.getCurrent().getStyles();
        styles.add(".helloVG .badge-background-" + color + " { background-color: #" + this.color + "; }");
    }

    public BadgePOJO createBadgePOJO() {
        return new BadgePOJO(getContent(), name, color);
    }

    @Override
    public int compareTo(Object o) {
        return getName().compareTo(((Badge) o).getName());
    }
}